﻿using AnimeLibrary.App_Code;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace AnimeLibrary
{
    public partial class library : System.Web.UI.Page
    {
        private int mPageSize = 15;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cmbSortBy.SelectedValue = "Title";
                if (Request.QueryString["sort"] != null)
                    cmbSortBy.SelectedValue = Request.QueryString["sort"];
                if (Request.QueryString["order"] != null)
                    rdblstOrderBy.SelectedValue = Request.QueryString["order"];

                cmbSortBy_SelectedIndexChanged(cmbSortBy, null);
            }
        }

        protected void rdblstOrderBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbSortBy_SelectedIndexChanged(txtSearch, null);
        }

        protected void cmbSortBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSortBy.SelectedIndex > -1)
                txtSearch_TextChanged(txtSearch, null);
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text.Length > 0)
                BindData(string.Format("SELECT * FROM tbl_anime_library WHERE ([Title] LIKE '%{0}%') ORDER BY {1} {2}", txtSearch.Text, cmbSortBy.SelectedValue, rdblstOrderBy.SelectedValue));
            else if (cmbSortBy.SelectedIndex > -1)
                BindData(string.Format("SELECT * FROM tbl_anime_library ORDER BY {0} {1}", cmbSortBy.SelectedValue, rdblstOrderBy.SelectedValue));
        }

        private void BindData(string commandText)
        {
            try
            {
                PagedDataSource dsPages = new PagedDataSource();

                DataTable dtAnimeLibrary = ReadOp.GetDataTable(Request.MapPath("App_Data\\AnimeLibrary.accdb"), commandText);

                dsPages.DataSource = dtAnimeLibrary.DefaultView;
                dsPages.AllowPaging = true;
                dsPages.PageSize = mPageSize;

                //============ ADDED NEW ============

                double totalPages = (dtAnimeLibrary.Rows.Count) / dsPages.PageSize;
                totalPages = Math.Ceiling(totalPages) + 1;

                //============ ADDED NEW ============

                int currentPage = 1;
                if ((Request.QueryString["page"] != null) && (!Request.QueryString["page"].Equals(string.Empty)))
                    currentPage = Convert.ToInt32(Request.QueryString["page"]);

                dsPages.CurrentPageIndex = (currentPage - 1);
                lblTopCurrentPage.Text = string.Format("{0} of {1}", currentPage, totalPages);
                lblBottomCurrentPage.Text = lblTopCurrentPage.Text;

                if (!dsPages.IsFirstPage)
                {
                    lnkTopPrevious.NavigateUrl = string.Format("{0}?page={1}&sort={2}&order={3}", Request.CurrentExecutionFilePath, (currentPage - 1), cmbSortBy.SelectedValue, rdblstOrderBy.SelectedValue);
                    lnkBottomPrevious.NavigateUrl = lnkTopPrevious.NavigateUrl;
                }
                else
                {
                    lnkTopPrevious.Visible = false;
                    lnkBottomPrevious.Visible = lnkTopPrevious.Visible;
                }

                if (!dsPages.IsLastPage)
                {
                    lnkTopNext.NavigateUrl = string.Format("{0}?page={1}&sort={2}&order={3}", Request.CurrentExecutionFilePath, (currentPage + 1), cmbSortBy.SelectedValue, rdblstOrderBy.SelectedValue);
                    lnkBottomNext.NavigateUrl = lnkTopNext.NavigateUrl;
                }
                else
                {
                    lnkTopNext.Visible = false;
                    lnkBottomNext.Visible = lnkTopNext.Visible;
                }

                rptAnimeLibraryList.DataSource = dsPages;
                rptAnimeLibraryList.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}